package weaver.interfaces.zhlx.duss.action.salesmodule.c4;

import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.C4Util;
import weaver.soa.workflow.request.RequestInfo;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-11-27
 * @Description: 销售合同流程归档节点将双章合同附件回传C4
 * @ModifyLog:
 **/
public class WeaXshtlcDocAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        writeLog(">>>>>WeaXshtlcDocAction>>>>>requestid=" + requestid);
        try {
            String message = "调用C4系统接口返回:";
            RecordSet rs = new RecordSet();
            rs.executeQuery("select uuid,ywxtddh,szht from " + tablename + " where requestid=? and htly=1", requestid);
            rs.next();
            String uuid = Util.null2String(rs.getString("uuid"));
            String ywxtddh = Util.null2String(rs.getString("ywxtddh"));
            String szht = Util.null2String(rs.getString("szht"));
            if (!"".equals(szht)) {
                rs.executeQuery("select imagefileid,imagefilename from DocImageFile where docid in (" + szht + ")");
                int counts = rs.getCounts();
                int count = 0;
                while (rs.next()) {
                    int imagefileid = Util.getIntValue(rs.getString("imagefileid"), -1);
                    String imagefilename = Util.null2String(rs.getString("imagefilename"));
                    writeLog(">>>>>WeaXshtlcDocAction>>>>>imagefileid=" + imagefileid);
                    writeLog(">>>>>WeaXshtlcDocAction>>>>>imagefilename=" + imagefilename);
                    InputStream inputStream = C4Util.getInputStreamById(imagefileid);
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024 * 100];
                    int bytesRead;
                    while ((bytesRead = inputStream.read(buffer)) != -1) {
                        byteArrayOutputStream.write(buffer, 0, bytesRead);
                    }
                    byteArrayOutputStream.flush();
                    JSONObject paramJson = new JSONObject();
                    paramJson.put("ParentObjectID", uuid);
                    paramJson.put("ContractID", ywxtddh);
                    paramJson.put("TypeCode", "10001");
                    String mimeType = "";
                    if (imagefilename.lastIndexOf(".") > -1) {
                        String filetype = imagefilename.substring(imagefilename.lastIndexOf(".") + 1);
                        mimeType = filetype.equalsIgnoreCase("pdf") ? "application/pdf" : "image/" + filetype;
                    }
                    paramJson.put("MimeType", mimeType);
                    paramJson.put("Binary", byteArrayOutputStream.toByteArray());
                    paramJson.put("Name", imagefilename);
                    paramJson.put("CategoryCode", "2");
                    Map<String, Object> dataMap = new HashMap<>();
                    dataMap.put("jkdymc", "WeaXshtlcDocAction");
                    dataMap.put("lcid", requestid);
                    dataMap.put("dylc", requestid);
                    writeLog(">>>>>WeaXshtlcDocAction>>>>>paramJson=" + paramJson);
                    JSONObject resultJson = C4Util.createHtfj(paramJson, dataMap);
                    writeLog(">>>>>WeaXshtlcDocAction>>>>>resultJson=" + resultJson);
                    String code = Util.null2String(resultJson.getString("code"));
                    if (!"success".equals(code)) {
                        String data = Util.null2String(resultJson.getString("data"));
                        message += Util.null2String(resultJson.getString("message"));
                        rs.executeUpdate("update " + tablename + " set htuuid=? where requestid=?", data, requestid);
                    }else {
                        count++;
                    }
                }
                if (counts != count) {
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }

            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaXshtlcDocAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}